package com.ankang.controller;

import com.ankang.entity.ChannelLevel;
import com.ankang.entity.ChannelStatVo;
import com.ankang.entity.TobaccoPackageOutbound;
import com.ankang.service.ChannelLevelService;
import com.ankang.service.TobaccoPackageOutboundService;
import com.ankang.service.impl.DisplayService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author mjiajia
 * @date 2025/9/1 11:48
 * @desciption
 */
@Controller
@RequestMapping("/channel")
public class ChannelLevelController {

    @Autowired
    private ChannelLevelService channelLevelService;

    @Autowired
    private DisplayService displayService;  // 新增

    @Autowired
    private TobaccoPackageOutboundService outboundService;  // 注入出库表服务

    // 跳转到列表页面
//    @GetMapping("/list")
//    public String list(Model model) {
//        List<ChannelLevel> channels = channelLevelService.list();
//        model.addAttribute("channels", channels);
//        return "channel/list"; // 对应templates目录下的页面
//    }


    // 跳转到列表页面（增加时间范围查询参数）
    @GetMapping("/list")
    public String list(Model model, @RequestParam(required = false) String timeRange) {
        // 1. 获取通道配置列表
        List<ChannelLevel> channels = channelLevelService.list();
        model.addAttribute("channels", channels);
        model.addAttribute("timeRange", timeRange);  // 回显查询条件

        // 2. 获取通道1-5的出库统计
        List<ChannelStatVo> channelStats = new ArrayList<>();
        for (int i = 1; i <= 5; i++) {
            ChannelStatVo stat = new ChannelStatVo();
            stat.setChannelNumber(i);

            // 根据时间范围查询统计数
            long count;
            if ("today".equals(timeRange)) {
                // 当日：查询今天0点到当前时间的记录
                Date now = new Date();
                // 定义日期格式：年-月-日
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                count = outboundService.lambdaQuery()
                        .eq(TobaccoPackageOutbound::getChannelNumber, i)
                        .like(TobaccoPackageOutbound::getCreateTime, sdf.format(now))
                        .count();
            } else {
                // 总计：查询所有记录
                count = outboundService.lambdaQuery()
                        .eq(TobaccoPackageOutbound::getChannelNumber, i)
                        .count();
            }

            stat.setPackageCount((int) count);
            channelStats.add(stat);
        }
        model.addAttribute("channelStats", channelStats);

        return "channel/list";
    }


    // 跳转到编辑页面
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable Integer id, Model model) {
        ChannelLevel channel = channelLevelService.getById(id);
        model.addAttribute("channel", channel);
        return "channel/edit";
    }

    // 保存修改
    @PostMapping("/update")
    public String update(ChannelLevel channel) {
        channelLevelService.updateById(channel);
        displayService.updateDisplay(channel);  // 配置更新后刷新显示器
        return "redirect:/channel/list"; // 保存后跳转回列表页
    }
}
